const db = require("../lib/conn_oracle")
const loginAndSession = require("../util/loginAndSession")

//用户目录信息
// exports.userDirs = (req,res)=>{
//     // console.log(req);
//     // let {username} = req.body
//     let {username} = req.query
//     // console.log(req.query);    
//     console.log(username);
//     let userDirsSql = `
//                     select u.personid,u.username,u.realname
//                     ,urm.roleid,ram.authorityid,adm.dirid
//                     ,d.title,d.parent,d.path,d.dirlevel
//                     ,d.icon,d.componentname,d.reporturl
//                     ,d.remark,d.dirname
//                     from c##wxw.vue_xfjcusers u
//                     inner join c##wxw.vue_xfjcUser_Role_middle urm
//                     on u.personid = urm.personid
//                     inner join c##wxw.vue_xfjcRole_Authority_middle ram
//                     on urm.roleid = ram.roleid
//                     inner join c##wxw.vue_xfjcAuthority_Dirs_middle adm
//                     on  ram.authorityid = adm.authorityid
//                     inner join c##wxw.vue_xfjcDirs d
//                     on adm.dirid = d.id
//                     where username = '${username}'
//                     `
//     let datas;
//     async function fun(){
//         let userDirs = await db.search2(userDirsSql)
//         // console.log(users);
//         datas = {userDirs};
//         if(userDirs.length){
//             res.json({
//                 status:1,
//                 msg:"登陆成功",
//                 datas,
//             });
//             //写入登录信息表
            
//         }else{
//             res.json({
//                 status:0,
//                 msg:"获取失败",
//                 datas
//             });
//         }        
//     } 
//     fun();
// }

//获取用户信息
exports.getChangedUserInfo = async (req,res)=>{
    // console.log(req.query);
    let {username} = req.query  
    let sqlParams = [username] 
    let resDatas
    let status
    let msg
    let newUserSql = `select * from c##wxw.vue_xfjcusers where username = :username`
    try{
        let changedNewUser = await db.search(newUserSql,sqlParams)
        resDatas = {changedNewUser}
        status = changedNewUser.code ? 0 :1
        msg = changedNewUser.code ? `查询失败 ${changedNewUser.code}` : "查询成功"
        res.json({
            status,
            msg,
            resDatas
        })
    }catch(err){
        status = 0
        msg = `查询失败 ${err}`
        res.json({
            status,
            msg
        })
    }
}

//修改用户收藏
exports.editCollectInfo = async (req,res)=>{
    // console.log(req.body)
    let {compObj,collectStatus} = req.body
    let {PERSONID,DIRID,PATH,COMPONENTNAME,TITLE} = compObj
    collectStatus = collectStatus === true ? true : false
    // console.log(compObj,collectStatus)
    let msg
    let resDatas 
    let status
    let usercollectData = [Number(PERSONID),Number(DIRID),PATH,COMPONENTNAME,TITLE]
    let userCollectSql = !collectStatus ? `
                                          insert into c##wxw.vue_xfjcuser_collect(id,personid,dirid,path,componentname,title)
                                          values('',:PERSONID,:DIRID,:PATH,:COMPONENTNAME,:TITLE)
                                         `
                                       : `delete c##wxw.vue_xfjcuser_collect 
                                          where personid = :PERSONID
                                            and dirid = :DIRID
                                            and path = :PATH
                                            and componentname = :COMPONENTNAME 
                                            and title = :TITLE                                      
                                         `
    let changeTypeStr = collectStatus ? "取消收藏" : "收藏"
    // console.log(userCollectSql)    
    try{
        let userCollect = await db.writeTab(userCollectSql,usercollectData)
        resDatas = {userCollect}
        status = resDatas.code ? 0 : 1
        msg = resDatas.code ? `${changeTypeStr}失败` : `${changeTypeStr}成功`
        res.json({
            status,
            msg,
            resDatas
        })

    }catch(err){
        res.json({
            status:0,
            msg:`${changeTypeStr}失败`
        })
    }
    
    
}

//获取用户收藏
exports.getUserCollect = async (req,res)=>{
    let {personid} = req.query
    personid = Number(personid)
    let userCollectSql = `select * from c##wxw.vue_xfjcuser_collect where personid = :personid`
    let resDatas
    let status
    let msg
    try{
        let userCollect = await db.search(userCollectSql,[personid])
        resDatas = {userCollect}
        status = userCollect.code ? 0 : 1
        msg = userCollect.code ? "用户收藏获取失败" : "用户收藏获取成功"
        res.json({
            status,
            msg,
            resDatas
        })
    }catch(err){
        res.json({
            status:0,
            msg:`用户收藏获取失败 ${err}`
        })
    }
}

//用户退出
exports.userLogout = async (req,res)=>{
    let {sessionid,personid,type} = req.body
    // console.log(sessionid,personid,type)    
    let clientIp = req.connection.remoteAddress 
    let loginLogAddDatas = [personid,clientIp,type,'SUCCESS']

    let sessionUpdateDatas = [sessionid]
    try{
        //写入退出日志及修改session状态
        loginAndSession.logoutSession(sessionUpdateDatas)        
        loginAndSession.addLog(loginLogAddDatas)
        res.json({
            status:1,
            msg:"退出成功"
        })
    }catch(err){
        console.log(err)        
    }
}

//续期token
exports.needRenewToken = async (req,res)=>{
    // console.log(req.body)
    res.json({
        status:1,
        msg:"续期成功"
    })
}

